{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "afd55886-5f5b-4794-838e-ef8179fb0394",
   "metadata": {},
   "source": [
    "##### **** These pip installs need to be adapted to use the appropriate release level. Alternatively, The venv running the jupyter lab could be pre-configured with a requirement file that includes the right release. Example for transform developers working from git clone:\n",
    "```\n",
    "make venv \n",
    "source venv/bin/activate \n",
    "pip install jupyterlab\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c45c3c6-e4d7-4e61-8de6-32d61f2ce695",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture\n",
    "## This is here as a reference only\n",
    "# Users and application developers must use the right tag for the latest from pypi\n",
    "#%pip install data-prep-toolkit[ray]\n",
    "#%pip install data-prep-toolkit-transforms[ededup]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "407fd4e4-265d-4ec7-bbc9-b43158f5f1f3",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "##### **** Configure the transform parameters. The set of dictionary keys holding EdedupTransform configuration for values are as follows: \n",
    "* doc_column - specifies name of the column containing documents\n",
    "* doc_id_column - specifies the name of the column containing a document id\n",
    "* use_snapshot - specifies that ededup execution starts with a set of pre-existing hashes, enabling incremental\n",
    "execution\n",
    "* snapshot_directory - specifies the directory for reading snapshots. If not provided, the default is\n",
    "`output_folder/snapshot`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebf1f782-0e61-485c-8670-81066beb734c",
   "metadata": {},
   "source": [
    "##### ***** Import required classes and modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2a12abc-9460-4e45-8961-873b48a9ab19",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dpk_ededup.ray import Ededup"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7234563c-2924-4150-8a31-4aec98c1bf33",
   "metadata": {},
   "source": [
    "##### ***** Setup runtime parameters for this transform and invoke the transform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1e4f64dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "11:35:26 INFO - exact dedup params are {'doc_column': 'contents', 'doc_id_column': 'document_id', 'use_snapshot': False, 'snapshot_directory': None, 'hash_cpu': 0.5, 'num_hashes': 2}\n",
      "11:35:26 INFO - pipeline id pipeline_id\n",
      "11:35:26 INFO - code location None\n",
      "11:35:26 INFO - number of workers 1 worker options {'num_cpus': 0.8, 'max_restarts': -1}\n",
      "11:35:26 INFO - actor creation delay 0\n",
      "11:35:26 INFO - job details {'job category': 'preprocessing', 'job name': 'ededup', 'job type': 'ray', 'job id': 'job_id'}\n",
      "11:35:26 INFO - data factory data_ is using local data access: input_folder - test-data/input output_folder - output\n",
      "11:35:26 INFO - data factory data_ max_files -1, n_sample -1\n",
      "11:35:26 INFO - data factory data_ Not using data sets, checkpointing False, max files -1, random samples -1, files to use ['.parquet'], files to checkpoint ['.parquet']\n",
      "11:35:26 INFO - Running locally\n",
      "WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n",
      "I0000 00:00:1733999728.143368 3403365 fork_posix.cc:75] Other threads are currently calling into gRPC, skipping fork() handlers\n",
      "I0000 00:00:1733999732.775454 3403365 fork_posix.cc:75] Other threads are currently calling into gRPC, skipping fork() handlers\n",
      "I0000 00:00:1733999733.942773 3403365 fork_posix.cc:75] Other threads are currently calling into gRPC, skipping fork() handlers\n",
      "I0000 00:00:1733999733.978314 3403365 fork_posix.cc:75] Other threads are currently calling into gRPC, skipping fork() handlers\n",
      "2024-12-12 11:35:36,072\tINFO worker.py:1777 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8265 \u001b[39m\u001b[22m\n",
      "\u001b[36m(orchestrate pid=38973)\u001b[0m 11:35:38 INFO - orchestrator started at 2024-12-12 11:35:38\n",
      "\u001b[36m(orchestrate pid=38973)\u001b[0m 11:35:38 INFO - Number of files is 1, source profile {'max_file_size': 0.034458160400390625, 'min_file_size': 0.034458160400390625, 'total_file_size': 0.034458160400390625}\n",
      "\u001b[36m(orchestrate pid=38973)\u001b[0m 11:35:38 INFO - Cluster resources: {'cpus': 12, 'gpus': 0, 'memory': 11.283737564459443, 'object_store': 2.0}\n",
      "\u001b[36m(orchestrate pid=38973)\u001b[0m 11:35:38 INFO - Number of workers - 1 with {'num_cpus': 0.8, 'max_restarts': -1} each\n",
      "\u001b[36m(orchestrate pid=38973)\u001b[0m 11:35:40 INFO - Completed 0 files (0.0%)  in 0.0 min. Waiting for completion\n",
      "\u001b[36m(orchestrate pid=38973)\u001b[0m 11:35:40 INFO - Completed processing 1 files in 0.002 min\n",
      "\u001b[36m(orchestrate pid=38973)\u001b[0m 11:35:40 INFO - done flushing in 0.002 sec\n",
      "11:35:50 INFO - Completed execution in 0.396 min, execution result 0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Ededup(input_folder=\"test-data/input\",\n",
    "    output_folder=\"output\",\n",
    "    run_locally= True,\n",
    "    ededup_hash_cpu= 0.5,\n",
    "    ededup_num_hashes= 2,\n",
    "    ededup_doc_column=\"contents\",\n",
    "    ededup_doc_id_column=\"document_id\").transform()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3df5adf-4717-4a03-864d-9151cd3f134b",
   "metadata": {},
   "source": [
    "##### **** The specified folder will include the transformed parquet files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7276fe84-6512-4605-ab65-747351e13a7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['output/snapshot', 'output/sample1.parquet', 'output/metadata.json']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import glob\n",
    "glob.glob(\"output/*\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "845a75cf-f4a9-467d-87fa-ccbac1c9beb8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
